package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.FeedTaskQualityDAO;
import cn.edu.scau.cmi.hombio.dao.FodderStorageDAO;
import cn.edu.scau.cmi.hombio.dao.FodderTakeDAO;
import cn.edu.scau.cmi.hombio.dao.FodderTypeDAO;
import cn.edu.scau.cmi.hombio.dao.UnitDAO;

import cn.edu.scau.cmi.hombio.domain.FeedTaskQuality;
import cn.edu.scau.cmi.hombio.domain.FodderStorage;
import cn.edu.scau.cmi.hombio.domain.FodderTake;
import cn.edu.scau.cmi.hombio.domain.FodderType;
import cn.edu.scau.cmi.hombio.domain.Unit;

import cn.edu.scau.cmi.hombio.service.FodderTypeService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for FodderType entities
 * 
 */

@Controller("FodderTypeRestController")

public class FodderTypeRestController {

	/**
	 * DAO injected by Spring that manages FeedTaskQuality entities
	 * 
	 */
	@Autowired
	private FeedTaskQualityDAO feedTaskQualityDAO;

	/**
	 * DAO injected by Spring that manages FodderStorage entities
	 * 
	 */
	@Autowired
	private FodderStorageDAO fodderStorageDAO;

	/**
	 * DAO injected by Spring that manages FodderTake entities
	 * 
	 */
	@Autowired
	private FodderTakeDAO fodderTakeDAO;

	/**
	 * DAO injected by Spring that manages FodderType entities
	 * 
	 */
	@Autowired
	private FodderTypeDAO fodderTypeDAO;

	/**
	 * DAO injected by Spring that manages Unit entities
	 * 
	 */
	@Autowired
	private UnitDAO unitDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for FodderType entities
	 * 
	 */
	@Autowired
	private FodderTypeService fodderTypeService;

	/**
	 * Save an existing FodderType entity
	 * 
	 */
	@RequestMapping(value = "/FodderType", method = RequestMethod.PUT)
	@ResponseBody
	public FodderType saveFodderType(@RequestBody FodderType foddertype) {
		fodderTypeService.saveFodderType(foddertype);
		return fodderTypeDAO.findFodderTypeByPrimaryKey(foddertype.getId());
	}

	/**
	* Save an existing FodderStorage entity
	* 
	*/
	@RequestMapping(value = "/FodderType/{foddertype_id}/fodderStorages", method = RequestMethod.PUT)
	@ResponseBody
	public FodderStorage saveFodderTypeFodderStorages(@PathVariable Integer foddertype_id, @RequestBody FodderStorage fodderstorages) {
		fodderTypeService.saveFodderTypeFodderStorages(foddertype_id, fodderstorages);
		return fodderStorageDAO.findFodderStorageByPrimaryKey(fodderstorages.getId());
	}

	/**
	* View an existing FeedTaskQuality entity
	* 
	*/
	@RequestMapping(value = "/FodderType/{foddertype_id}/feedTaskQualities/{feedtaskquality_id}", method = RequestMethod.GET)
	@ResponseBody
	public FeedTaskQuality loadFodderTypeFeedTaskQualities(@PathVariable Integer foddertype_id, @PathVariable Integer related_feedtaskqualities_id) {
		FeedTaskQuality feedtaskquality = feedTaskQualityDAO.findFeedTaskQualityByPrimaryKey(related_feedtaskqualities_id, -1, -1);

		return feedtaskquality;
	}

	/**
	* View an existing FodderStorage entity
	* 
	*/
	@RequestMapping(value = "/FodderType/{foddertype_id}/fodderStorages/{fodderstorage_id}", method = RequestMethod.GET)
	@ResponseBody
	public FodderStorage loadFodderTypeFodderStorages(@PathVariable Integer foddertype_id, @PathVariable Integer related_fodderstorages_id) {
		FodderStorage fodderstorage = fodderStorageDAO.findFodderStorageByPrimaryKey(related_fodderstorages_id, -1, -1);

		return fodderstorage;
	}

	/**
	* View an existing FodderTake entity
	* 
	*/
	@RequestMapping(value = "/FodderType/{foddertype_id}/fodderTakes/{foddertake_id}", method = RequestMethod.GET)
	@ResponseBody
	public FodderTake loadFodderTypeFodderTakes(@PathVariable Integer foddertype_id, @PathVariable Integer related_foddertakes_id) {
		FodderTake foddertake = fodderTakeDAO.findFodderTakeByPrimaryKey(related_foddertakes_id, -1, -1);

		return foddertake;
	}

	/**
	* Create a new FodderTake entity
	* 
	*/
	@RequestMapping(value = "/FodderType/{foddertype_id}/fodderTakes", method = RequestMethod.POST)
	@ResponseBody
	public FodderTake newFodderTypeFodderTakes(@PathVariable Integer foddertype_id, @RequestBody FodderTake foddertake) {
		fodderTypeService.saveFodderTypeFodderTakes(foddertype_id, foddertake);
		return fodderTakeDAO.findFodderTakeByPrimaryKey(foddertake.getId());
	}

	/**
	* Save an existing FeedTaskQuality entity
	* 
	*/
	@RequestMapping(value = "/FodderType/{foddertype_id}/feedTaskQualities", method = RequestMethod.PUT)
	@ResponseBody
	public FeedTaskQuality saveFodderTypeFeedTaskQualities(@PathVariable Integer foddertype_id, @RequestBody FeedTaskQuality feedtaskqualities) {
		fodderTypeService.saveFodderTypeFeedTaskQualities(foddertype_id, feedtaskqualities);
		return feedTaskQualityDAO.findFeedTaskQualityByPrimaryKey(feedtaskqualities.getId());
	}

	/**
	* Show all FodderTake entities by FodderType
	* 
	*/
	@RequestMapping(value = "/FodderType/{foddertype_id}/fodderTakes", method = RequestMethod.GET)
	@ResponseBody
	public List<FodderTake> getFodderTypeFodderTakes(@PathVariable Integer foddertype_id) {
		return new java.util.ArrayList<FodderTake>(fodderTypeDAO.findFodderTypeByPrimaryKey(foddertype_id).getFodderTakes());
	}

	/**
	* Delete an existing FeedTaskQuality entity
	* 
	*/
	@RequestMapping(value = "/FodderType/{foddertype_id}/feedTaskQualities/{feedtaskquality_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteFodderTypeFeedTaskQualities(@PathVariable Integer foddertype_id, @PathVariable Integer related_feedtaskqualities_id) {
		fodderTypeService.deleteFodderTypeFeedTaskQualities(foddertype_id, related_feedtaskqualities_id);
	}

	/**
	* Select an existing FodderType entity
	* 
	*/
	@RequestMapping(value = "/FodderType/{foddertype_id}", method = RequestMethod.GET)
	@ResponseBody
	public FodderType loadFodderType(@PathVariable Integer foddertype_id) {
		return fodderTypeDAO.findFodderTypeByPrimaryKey(foddertype_id);
	}

	/**
	* Create a new FodderType entity
	* 
	*/
	@RequestMapping(value = "/FodderType", method = RequestMethod.POST)
	@ResponseBody
	public FodderType newFodderType(@RequestBody FodderType foddertype) {
		fodderTypeService.saveFodderType(foddertype);
		return fodderTypeDAO.findFodderTypeByPrimaryKey(foddertype.getId());
	}

	/**
	* Delete an existing FodderTake entity
	* 
	*/
	@RequestMapping(value = "/FodderType/{foddertype_id}/fodderTakes/{foddertake_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteFodderTypeFodderTakes(@PathVariable Integer foddertype_id, @PathVariable Integer related_foddertakes_id) {
		fodderTypeService.deleteFodderTypeFodderTakes(foddertype_id, related_foddertakes_id);
	}

	/**
	* Create a new Unit entity
	* 
	*/
	@RequestMapping(value = "/FodderType/{foddertype_id}/unit", method = RequestMethod.POST)
	@ResponseBody
	public Unit newFodderTypeUnit(@PathVariable Integer foddertype_id, @RequestBody Unit unit) {
		fodderTypeService.saveFodderTypeUnit(foddertype_id, unit);
		return unitDAO.findUnitByPrimaryKey(unit.getId());
	}

	/**
	* Show all FodderStorage entities by FodderType
	* 
	*/
	@RequestMapping(value = "/FodderType/{foddertype_id}/fodderStorages", method = RequestMethod.GET)
	@ResponseBody
	public List<FodderStorage> getFodderTypeFodderStorages(@PathVariable Integer foddertype_id) {
		return new java.util.ArrayList<FodderStorage>(fodderTypeDAO.findFodderTypeByPrimaryKey(foddertype_id).getFodderStorages());
	}

	/**
	* Show all FeedTaskQuality entities by FodderType
	* 
	*/
	@RequestMapping(value = "/FodderType/{foddertype_id}/feedTaskQualities", method = RequestMethod.GET)
	@ResponseBody
	public List<FeedTaskQuality> getFodderTypeFeedTaskQualities(@PathVariable Integer foddertype_id) {
		return new java.util.ArrayList<FeedTaskQuality>(fodderTypeDAO.findFodderTypeByPrimaryKey(foddertype_id).getFeedTaskQualities());
	}

	/**
	* View an existing Unit entity
	* 
	*/
	@RequestMapping(value = "/FodderType/{foddertype_id}/unit/{unit_id}", method = RequestMethod.GET)
	@ResponseBody
	public Unit loadFodderTypeUnit(@PathVariable Integer foddertype_id, @PathVariable Integer related_unit_id) {
		Unit unit = unitDAO.findUnitByPrimaryKey(related_unit_id, -1, -1);

		return unit;
	}

	/**
	* Save an existing FodderTake entity
	* 
	*/
	@RequestMapping(value = "/FodderType/{foddertype_id}/fodderTakes", method = RequestMethod.PUT)
	@ResponseBody
	public FodderTake saveFodderTypeFodderTakes(@PathVariable Integer foddertype_id, @RequestBody FodderTake foddertakes) {
		fodderTypeService.saveFodderTypeFodderTakes(foddertype_id, foddertakes);
		return fodderTakeDAO.findFodderTakeByPrimaryKey(foddertakes.getId());
	}

	/**
	* Delete an existing Unit entity
	* 
	*/
	@RequestMapping(value = "/FodderType/{foddertype_id}/unit/{unit_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteFodderTypeUnit(@PathVariable Integer foddertype_id, @PathVariable Integer related_unit_id) {
		fodderTypeService.deleteFodderTypeUnit(foddertype_id, related_unit_id);
	}

	/**
	* Create a new FeedTaskQuality entity
	* 
	*/
	@RequestMapping(value = "/FodderType/{foddertype_id}/feedTaskQualities", method = RequestMethod.POST)
	@ResponseBody
	public FeedTaskQuality newFodderTypeFeedTaskQualities(@PathVariable Integer foddertype_id, @RequestBody FeedTaskQuality feedtaskquality) {
		fodderTypeService.saveFodderTypeFeedTaskQualities(foddertype_id, feedtaskquality);
		return feedTaskQualityDAO.findFeedTaskQualityByPrimaryKey(feedtaskquality.getId());
	}

	/**
	* Create a new FodderStorage entity
	* 
	*/
	@RequestMapping(value = "/FodderType/{foddertype_id}/fodderStorages", method = RequestMethod.POST)
	@ResponseBody
	public FodderStorage newFodderTypeFodderStorages(@PathVariable Integer foddertype_id, @RequestBody FodderStorage fodderstorage) {
		fodderTypeService.saveFodderTypeFodderStorages(foddertype_id, fodderstorage);
		return fodderStorageDAO.findFodderStorageByPrimaryKey(fodderstorage.getId());
	}

	/**
	* Save an existing Unit entity
	* 
	*/
	@RequestMapping(value = "/FodderType/{foddertype_id}/unit", method = RequestMethod.PUT)
	@ResponseBody
	public Unit saveFodderTypeUnit(@PathVariable Integer foddertype_id, @RequestBody Unit unit) {
		fodderTypeService.saveFodderTypeUnit(foddertype_id, unit);
		return unitDAO.findUnitByPrimaryKey(unit.getId());
	}

	/**
	* Delete an existing FodderStorage entity
	* 
	*/
	@RequestMapping(value = "/FodderType/{foddertype_id}/fodderStorages/{fodderstorage_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteFodderTypeFodderStorages(@PathVariable Integer foddertype_id, @PathVariable Integer related_fodderstorages_id) {
		fodderTypeService.deleteFodderTypeFodderStorages(foddertype_id, related_fodderstorages_id);
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* Get Unit entity by FodderType
	* 
	*/
	@RequestMapping(value = "/FodderType/{foddertype_id}/unit", method = RequestMethod.GET)
	@ResponseBody
	public Unit getFodderTypeUnit(@PathVariable Integer foddertype_id) {
		return fodderTypeDAO.findFodderTypeByPrimaryKey(foddertype_id).getUnit();
	}

	/**
	* Delete an existing FodderType entity
	* 
	*/
	@RequestMapping(value = "/FodderType/{foddertype_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteFodderType(@PathVariable Integer foddertype_id) {
		FodderType foddertype = fodderTypeDAO.findFodderTypeByPrimaryKey(foddertype_id);
		fodderTypeService.deleteFodderType(foddertype);
	}

	/**
	* Show all FodderType entities
	* 
	*/
	@RequestMapping(value = "/FodderType", method = RequestMethod.GET)
	@ResponseBody
	public List<FodderType> listFodderTypes() {
		return new java.util.ArrayList<FodderType>(fodderTypeService.loadFodderTypes());
	}
}